﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BusinessObjects;
using System.IO;
using DigiLib;

public partial class controls_admin_ModuleAdd : Admin_Controls_Base {
    protected void Page_Load(object sender, EventArgs e) {
        base.removeHGC(divMessage);
        if (!IsPostBack) {
            try {
                base.bindDDL(ddlType, BO_portal_module_types.SelectAll(1), "TYPE_DESCRIPTION", "MODULE_TYPE_ID", "-----", "0");
                base.bindDDL(ddlCategories, BO_portal_categories.Inside(0), "FULL_CATEGORY_NAME", "CATEGORY_ID", "-----", "0");
                base.bindDDL(ddlLocation, BO_portal_locations.SelectAll(1), "LOCATION_NAME", "LOCATION_ID", "-----", "0");
                base.bindLSB(lbxCategories, BO_portal_categories.SelectAll(), "FULL_CATEGORY_NAME", "CATEGORY_ID");
                if (Category_ID > 0)
                    lbxCategories.SelectedValue = Category_ID.ToString();
                txtSDHome.Text = SqlUtils.GetNextOrderDisplay("portal_modules").ToString();
            } catch (Exception ex) {
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
            }
        }
    }
    protected void btnModuleAction_Click(object sender, EventArgs e) {
        try {
            if (lbxCategories.Items.Count <= 0) { divMessage.InnerHtml = MessageBox.Warning("Cảnh Báo", "Yêu cầu chọn danh mục hiển thị (hiển thị tại)"); return; }
            string filename = string.Empty;
            int type_id = Convert.ToInt32(ddlType.SelectedValue);
            string title = txtTitle.Text.Trim();
            int category_id = Convert.ToInt32(ddlCategories.SelectedValue);
            string position = ddlLocation.SelectedValue;
            int sort_display = Convert.ToInt32(txtSDHome.Text.Trim());
            string readMore = "";// txtLink.Text.Trim();
            int page_size = Convert.ToInt32(txtPageSize.Text);
            bool isHome = cboDisDetails.Checked;
            bool isDetails = cboDisDetails.Checked;
            int location = Convert.ToInt32(ddlLocation.SelectedValue);
            DataTable admin = (DataTable)Session["Admin"];
            string strresult = BO_portal_modules.Insert(type_id, category_id.ToString(), title, filename, readMore, page_size, isDetails, isHome, sort_display, DateTime.Now, "tinhcd", DateTime.Now, "tinhcd", 1);            
            if (strresult != "0") {
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thêm mới thành công!");
                int module_id = Convert.ToInt32(strresult);
                foreach (ListItem item in lbxCategories.Items) {
                    if (item.Selected) {
                        BO_portal_modules_sort.Insert(module_id, Convert.ToInt32(item.Value), location, sort_display, true);
                    }
                }
            } else
                divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi trong quá trình xử lý!");
        } catch (Exception ex) {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát sinh lỗi như sau: " + ex.Message);
        }
    }
}
